package com.frame.demo.app;

import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.annotation.DrawableRes;
import android.support.annotation.IdRes;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.frame.demo.R;
import com.frame.demo.injector.component.ActivityComponent;
import com.frame.demo.injector.moudle.ActivityModule;
import com.frame.demo.rest.RestError;

/**
 * @author yuefeng
 * @version 3.3.1
 * @created at 15/12/22 下午4:59
 */
public abstract class TitleBaseActivity extends BaseFragmentActivity implements ActivityHelper {
    private LinearLayout mContainerLayout;
    private ViewGroup mContentView;
    protected SwipeRefreshLayout mSwipeRefresh;
    private Toolbar mToolbar;
    private TextView mRightText;
    private TextView mTitleText;
    private ActivityComponent mActivityComponent;
    private ActivityHelper mActivityHelper;
    protected Bundle mSavedInstanceState;

    @Override
    protected final void onCreate(Bundle savedInstanceState) {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        setComponent();
        super.onCreate(savedInstanceState);
        ActivityManager.getInstance()
                .pushActivity(this);
        setContentView(setRefreshing() ? R.layout.base_container_add_refresh_layout : R.layout.base_container_layout);
        mContainerLayout = (LinearLayout) super.findViewById(R.id.ll_container);
        mContentView = (ViewGroup) getLayoutInflater().inflate(inflateContentView(), mContainerLayout, false);
        if (setRefreshing()) {
            mSwipeRefresh = (SwipeRefreshLayout) super.findViewById(R.id.refresh);
            mSwipeRefresh.setOnRefreshListener(() -> {
                refreshData();
            });
            mSwipeRefresh.setColorSchemeResources(R.color.colorPrimary);
        }

        mToolbar = ((Toolbar) super.findViewById(R.id.toolbar));
        mTitleText = ((TextView) super.findViewById(R.id.tv_title));
        if (null != mToolbar) {
            mToolbar.setTitle(null);
            setSupportActionBar(mToolbar);
            mToolbar.setNavigationIcon(R.mipmap.ic_arrows_back);
            mToolbar.setNavigationOnClickListener(v -> onNavigationClick());
        }

        this.mActivityHelper = new ActivityHelperImpl(mContainerLayout, this, mContentView);
        /** 初始化 activity */
        mSavedInstanceState = savedInstanceState;
        initActivity(mContentView);

    }

    protected void onNavigationClick() {
        onBackPressed();
    }


    @Override
    public View findViewById(@IdRes int id) {
        return mContentView.findViewById(id);
    }

    /**
     * 初始化注入器
     */
    protected void setComponent() {
        mActivityComponent = DaggerActivityComponent.builder()
                .applicationComponent(MyApplication.getInstance().component())
                .activityModule(new ActivityModule(this))
                .build();
        inject(mActivityComponent);
    }

    public abstract void initActivity(View view);

    public abstract void inject(ActivityComponent pActivityComponent);

    /**
     * 得到dagger的component的实例
     *
     * @return
     */
    public ActivityComponent getActivityComponent() {
        return mActivityComponent;
    }

    /**
     * @return 设置content layout id
     */
    public abstract int inflateContentView();

    public boolean setRefreshing() {
        return false;
    }

    protected void refreshData() {
    }


    public void setHeaderTitle(@StringRes int resId) {
        if (null != mTitleText) {
            mTitleText.setText(getResources().getString(resId));
        }
    }

    public void setHeaderTitle(String pTitleStr) {
        if (null != mTitleText) {
            mTitleText.setText(pTitleStr);
        }
    }

    /**
     * 设置右button内容和点击效果
     *
     * @author LeiYu
     *
     * @param pText 右button内容
     * @param pUrl 跳转的url（h5专用）
     *
     * created at 2016/3/23 11:13
     */
    public void showRightTextView(String pText, String pUrl) {
        mRightText = (TextView) super.findViewById(R.id.tv_right_text);
        mRightText.setVisibility(View.VISIBLE);
        mRightText.setText(pText);
        mRightText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(pUrl != null && !TextUtils.isEmpty(pUrl)) {
                    onRightTextViewClick(pUrl);
                } else {
                    onRightTextViewClick();
                }
            }
        });
    }

    public void showRightTextView(String pText) {
        showRightTextView(pText, null);
    }

    public void onRightTextViewClick(String pUrl) {
    }

    public void onRightTextViewClick() {
    }

    public Toolbar getToolbar() {
        return mToolbar;
    }


    public void commonExceptionDispose(RestError pRestError) {
        mActivityHelper.commonExceptionDispose(pRestError);

    }

    public void showContent() {
        mActivityHelper.showContent();
        if (setRefreshing()) {
            mSwipeRefresh.setRefreshing(false);
        }
    }

    /**
     * 显示页面内部进度提示
     */
    @Override
    public void showInPageProgressView() {
        mActivityHelper.showInPageProgressView();
    }

    public void showErrorView(@Nullable String pErrorMes) {
        showErrorView(pErrorMes, R.mipmap.icon_no_network);
    }

    @Override
    public void showErrorView(@Nullable String pErrorMes, @DrawableRes int pErrorIconRes) {
        mActivityHelper.showErrorView(pErrorMes, pErrorIconRes);
        if (setRefreshing()) {
            mSwipeRefresh.setRefreshing(false);
        }
    }

    /**
     * 根据异常类型，加载不同布局
     *
     * @param pRestError
     */
    @Override
    public void showErrorView(RestError pRestError) {
        mActivityHelper.showErrorView(pRestError);
        if (setRefreshing()) {
            mSwipeRefresh.setRefreshing(false);
        }
    }

    //适用自己指定错误页面显示位置
    public void showErrorView(ViewGroup pViewGroupParent, String pErrorMes, @IdRes int pErrorIconRes) {
        if (null == pViewGroupParent) return;
        mActivityHelper.showErrorView(pViewGroupParent, pErrorMes, pErrorIconRes);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
